package com.web.portal.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.web.portal.entity.AdvisorMeeting;
import com.web.portal.entity.Lab;
import com.web.portal.entity.LabPolicy;
import com.web.portal.exception.PortalException;
import com.web.portal.service.AdvisorMeetingService;

@Controller
public class AdvisorMeetingController {
	private static final Logger LOG =Logger.getLogger(AdvisorMeetingController.class);

@Autowired
AdvisorMeetingService advisorMeetingService;

public AdvisorMeetingService getAdvisorMeetingService() {
	return advisorMeetingService;
}

public void setAdvisorMeetingService(AdvisorMeetingService advisorMeetingService) {
	this.advisorMeetingService = advisorMeetingService;
}
@RequestMapping(value="/admin/advisormeeting.do")
public String getAdvisorMeeting(Map<String, Object> model) throws PortalException{
	AdvisorMeeting advisorMeeting= new AdvisorMeeting();
	List<AdvisorMeeting > advisorMeetingLists= advisorMeetingService.getAllAdvisorMeeting(advisorMeeting);
	AdvisorMeeting a=null;
	if(!advisorMeetingLists.isEmpty()){
		a=advisorMeetingLists.get(0);
	}
	List<AdvisorMeeting>advisorMeetingList= new ArrayList<AdvisorMeeting>();
	advisorMeetingList.add(a);
	model.put("advisorMeetingList", advisorMeetingList);
	LOG.debug("returning advisor meeting page...........................");
	return "advisor_meeting_view";
}
@RequestMapping (value="/admin/getadvisoraddpage.do", method=RequestMethod.GET)
public String getAddPage(Map model) throws PortalException {
	model.put("advisormeeting",new AdvisorMeeting());
	LOG.debug("getting add page for advisor meeting...........................");
	return "add_advisor_meeting_page";
}
@RequestMapping (value="/admin/addadvisormeeting.do", method=RequestMethod.POST)
public String addAdvisorMeeting(@Valid @ModelAttribute("advisormeeting") final AdvisorMeeting advisorMeeting, BindingResult bindingResult){
	if(!bindingResult.hasErrors()){
		/*
		 * save it to database
		 * 
		 */
		advisorMeetingService.createOrSave(advisorMeeting);
		LOG.debug("successfully added the advisor meeting page..........................");
		return "redirect:/admin/advisormeeting.do";
	}
	LOG.debug("something wrong on adding so returning to add page......................");
	return "redirect:/admin/getadvisoraddpage.do";
}
@RequestMapping(value="/admin/geteditpage.do", method=RequestMethod.GET)
public String getEditPage(@RequestParam ("id") final String id, Map model) throws PortalException{
	if(id!=null){
		AdvisorMeeting advisorMeeting=advisorMeetingService.getAdvisorMeeting(id);
		model.put("advisormeeting", advisorMeeting);
		
		return "edit_advisor_page";
	}
	LOG.debug("Something wrong and unable to load edit page...............");
	return "redirect:/admin/advisormeeting.do";
}
@RequestMapping(value = "/admin/advisormeetingeditsubmit.do", method = RequestMethod.POST)
public String editLabPolicy(
		@Valid @ModelAttribute("advisormeeting") final AdvisorMeeting advisorMeeting , BindingResult result) {
	if(advisorMeeting!=null){
		AdvisorMeeting thisAdvisorMeeting=advisorMeetingService.getAdvisorMeeting(advisorMeeting.getId());
		/*
		 * Update
		 */
		thisAdvisorMeeting.setId(advisorMeeting.getId());
		thisAdvisorMeeting.setIntentionAdvisiongTitle(advisorMeeting.getIntentionAdvisiongTitle());
		thisAdvisorMeeting.setLinkCSAndMathAndSecurityReqs(advisorMeeting.getLinkCSAndMathAndSecurityReqs());
		thisAdvisorMeeting.setLinkCSAndMathReqs(advisorMeeting.getLinkCSAndMathReqs());
		thisAdvisorMeeting.setLinkCSAndSecurityReqs(advisorMeeting.getLinkCSAndSecurityReqs());
		thisAdvisorMeeting.setLinkCSReqs(advisorMeeting.getLinkCSReqs());
		thisAdvisorMeeting.setLinkISReqs(advisorMeeting.getLinkISReqs());
		thisAdvisorMeeting.setLinkITReqs(advisorMeeting.getLinkITReqs());
		thisAdvisorMeeting.setSteps(advisorMeeting.getSteps());
		/*
		 * save it
		 */
		advisorMeetingService.createOrSave(thisAdvisorMeeting);
		LOG.debug("Successfully edited and persisted the page............................");
		return "redirect:/admin/advisormeeting.do";
	}
	LOG.debug("unable to persist so returning back to edit page..............................");
	return "redirect:/admin/geteditpage.do";
}
}
